package com.stupro.cjlu.manager;

import com.stupro.cjlu.bo.UserInfoBO;
import com.stupro.cjlu.constant.UserType;
import com.stupro.cjlu.dao.AdmainDAO;
import com.stupro.cjlu.dao.StudentsDAO;
import com.stupro.cjlu.dao.TeacherDAO;
import com.stupro.cjlu.entity.Students;
import org.springframework.stereotype.Component;

@Component
public class UserManager {
    private final AdmainDAO admainDAO;
    private final TeacherDAO teacherDAO;
    private final StudentsDAO studentsDAO;

    public UserManager(AdmainDAO admainDAO, TeacherDAO teacherDAO, StudentsDAO studentsDAO) {
        this.admainDAO = admainDAO;
        this.teacherDAO = teacherDAO;
        this.studentsDAO = studentsDAO;
    }

    public UserInfoBO getUserInfoByUsername(String username, Integer userType) {
        if (userType == UserType.STUDENT) {
            return UserInfoBO.fromStudent(studentsDAO.getByNumber(username));
        } else if (userType == UserType.TEACHER) {
            return UserInfoBO.fromTeacher(teacherDAO.getByNumber(username));
        } else if (userType == UserType.ADMIN) {
            return UserInfoBO.fromAdmin(admainDAO.getByUsername(username));
        }

        return null;
    }

    public void updateStudentLastLoginTime(String stuUserName) {
        Students entity = studentsDAO.getByNumber(stuUserName);
        if (entity == null) {
            return;
        }

        //entity.setLastLoginTime(new Date());
        studentsDAO.update(entity);
    }
}
